﻿/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : login.js                                 
*  Description  : 로그인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-08  김효순    Created.
*  2012-04-18  김효순    Complete.
*******************************************************************************/

jQuery(function($){
	// 아이디
	function clsId()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "아이디를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsId.prototype = {
		_init: function(){
			var $layer = $("div#container section.member input:text:eq(0)");
			
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if(this.getValue() == ""){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
	}
	
	// 아이디저장
	function clsSaveId()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsSaveId.prototype = {
		_init: function(){
			var $layer = $("div#container section.member input:checkbox");
			
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, setCheck: function(){
			this._HTMLDom["$layer"].attr("checked", "checked");
		}
		, getCheck: function(){
			return this._HTMLDom["$layer"].is(":checked");
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 비밀번호
	function clsPasswd()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "비밀번호를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPasswd.prototype = {
		_init: function(){
			var $layer = $("div#container section.member input:text:eq(1)");
			
			var szClass = $layer.attr("class");
			var szStyle = $layer.attr("style");
			
			$layer.before("<input type='password' class='" + szClass + "' style='" + szStyle + "' />").remove();
			
			$layer = $("div#container section.member input:password");
			
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if(this.getValue() == ""){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
	}
	
	function clsComm()
	{
		// 로그인
		this.loginInfo = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, getLoginInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "login";
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename": serviceName,
				"listcount":10,
				"currpageno":1
			}, {
				"userId":  arg["아이디"]
				, "pw": arg["비밀번호"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				thisClass.loginInfo = {};
				
				if( ret["msp"]["contents"]["serviceResult"] == "00000"){
					thisClass.loginInfo["아이디"] = ret["msp"]["contents"]["items"][0]["userNo"];
				}
				
				document.objControl.finishLoginInfo(res);
			});
		}
		, getLoginData: function(){
			return this.loginInfo;
		}
		
	}
	
	
	function clsControl()
	{
		this.objComm = null;
		this.objId = null;
		this.objSaveId = null;
		this.objPasswd = null;
		
		this.q_url = document.objUtil.getParam("q_url");
		this.q_userid = document.objUtil.getParam("q_userid");
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objId = new clsId();
			this.objSaveId = new clsSaveId();
			this.objPasswd = new clsPasswd();
			
			if(document.objUtil.getCookie("member_id")){
				if(this.q_url) document.location.href = this.q_url;
				else document.location.href = _ROOT;
					
				return;
			}
			
			this._bind();
			
			if(this.q_userid){
				this.objId.setValue(this.q_userid);
			}else if(document.objUtil.getCookie("save_id")){
				this.objId.setValue(document.objUtil.getCookie("save_id"));
				this.objSaveId.setCheck();
			}
		}
		, _bind: function(){
			var thisClass = this;
			
			$("div.top_menu").find("a").attr("id", "idPermissionLink");
			
			// 로그인
			$("div#container section.member img[alt='로그인']").click(function(){
				thisClass.login();
			});
			
			// 엔터 체크
			this.objPasswd.$getLayer().keydown(function(e){
				e = e || event;
				
				if(e.keyCode == 13){
					thisClass.login();
				}
			});
			
			$("div#container section.member").on("click", "input[alt='회원가입'], input[alt='아이디/비밀번호 찾기']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "회원가입":
						thisClass.goMemberJoinPage();
						break;
					case "아이디/비밀번호 찾기":
						thisClass.goFindIdPwPage();
						break;
				}
			});
		}
		// 로그인
		, login: function(){
			if(!this.objId.valid()){
				alert(this.objId.getError());
				this.objId.focus();
				return;
			}else if(!this.objPasswd.valid()){
				alert(this.objPasswd.getError());
				this.objPasswd.focus();
				return;
			}
			
			document.objGlobal.spinStart("sending...");
			this.objComm.getLoginInfo({
				"아이디": this.objId.getValue()
				, "비밀번호": this.objPasswd.getValue()
			});
		}
		// 로그인 완료
		, finishLoginInfo: function(ret){
			document.objGlobal.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisData = this.objComm.getLoginData();
			var saveId;
			
			if(this.objSaveId.getCheck()) saveId = true;
			
			document.objGlobal.loginProc({
				"아이디": thisData["아이디"]
				, "아이디저장": saveId
			});
			
			if(this.q_url){
				document.location.href = this.q_url;
			}else{
				document.location.href = _ROOT + "/index/index.html";
			}
		}
		// 회원가입 이동
		, goMemberJoinPage: function(){
			var szLink = _ROOT + "/member/join_01.html";
			document.location.href = szLink;
		}
		// 아이디/비밀번호 찾기 이동
		, goFindIdPwPage: function(){
			var szLink = _ROOT + "/member/idpw.html";
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
});